import util from '@/libs/util';
export default {
    data () {
        return {
            columns: [{
                type: 'selection',
                key: '#'
            }, {
                title: '支出/收入名称',
                key: 'name'
            }, {
                title: '支出/收入金额',
                key: 'payAmt',
                render: (h, params) => {
                    let pay_amt = params.row.payAmt;
                    let pay_type = params.row.payType;
                    if (pay_type == 'IN') {
                        return h('span', {
                            style: {
                                color: 'green'
                            }
                        }, '+' + pay_amt);
                    } else if (pay_type == 'OUT') {
                        return h('span', {
                            style: {
                                'color': 'red'
                            }
                        }, '-' + pay_amt);
                    }
                }
            }, {
                title: '支出/收入类型',
                key: 'f_type'
            }, {
                title: '支出/收入人',
                key: 'payWho'
            }, {
                title: '支出/收入时间',
                key: 'payTime',
                render: (h, params) => {
                    return h('span', {}, util.getDate(params.row.payTime, 'time'));
                }
            }, {
                title: '操作',
                key: 'opt',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.formData = params.row;
                                    if (this.formData['payTime'] != null && this.formData['payTime'] != '' && this.formData['payTime'] != undefined) { this.formData['payTime'] = util.getDate(this.formData.payTime, 'time'); }
                                    this.addOrEditVisible = true;
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    let id = params.row.id;
                                    this.doDel(id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            dataList: [],
            formData:{},
            queryData: {
                type: '',
                name: '',
                startTime: '',
                endTime: '',
                payType: '',
                payWho: ''
            },
            addOrEditVisible: false,
            finaceList: [],
            selection: []
        };
    },
    mounted () {
        this.loadList();
        util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
            {CODE: 'FINANCE_TYPE'}
        )).then(res => {
            if (res.data.code == 200) {
                var data = res.data.data;
                this.finaceList = data;
            }
        });
    },
    methods: {
        loadList: function () {
            let queryData = this.queryData;
            util.ajax.post('/busine/finance/listBySql', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                type: queryData.type,
                name: queryData.name,
                startTime: queryData.startTime,
                endTime: queryData.endTime,
                payType: queryData.payType,
                payWho: queryData.payWho

            })).then(res => {
                if (res.data.code == 200) {
                    this.dataList = res.data.data.list;
                    this.total = res.data.data.total;
                } else {
                    this.$Message.error(res.data.message);
                }
            });
        },
        delBath: function () {
            let s = this.selection;
            if (s == null || s == '' || s == undefined || s.length <= 0) {
                this.$Message.warning('请选择删除项');
                return;
            }
            let ids = [];
            for (let i in s) {
                ids.push(s[i].id);
            }
            this.doDel(ids);
        },
        doSave: function () {
            let data = this.formData;
            if (data['payTime'] != null && data['payTime'] != '' && data['payTime'] != undefined)data['payTime'] = util.getDate(data.payTime, 'time');
            util.ajax.post('/busine/finance/doSave', util.toFormData(data)).then(res => {
                if (res.data.code == 200) {
                    this.$Message.success(res.data.data);
                    this.loadList();
                } else {
                    this.$Message.error(res.data.message);
                }
            });
        },
        doDel: function (ids) {
            this.$Modal.confirm({
                content: '确定删除?',
                onOk: () => {
                    util.ajax.post('/busine/finance/doDel', util.toFormData({
                        ids: ids
                    })).then(res => {
                        if (res.data.code == 200) {
                            this.$Message.success(res.data.data);
                            this.loadList();
                        } else {
                            this.$Message.error(res.data.message);
                        }
                    });
                },
                onCancel: () => {

                }
            });
        },
        selectionChange: function (selection) {
            this.selection = selection;
        },
        showAdd:function () {
            this.addOrEditVisible=true;
            this.formData={
                    type: '',
                    name: '',
                    payType: '',
                    payTime: '',
                    payAmt: 0,
                    payWho: ''
            };
        }
    }
};
